﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Net_SCADA;

namespace WZXC_GBL_QUERY
{
    public partial class Form_QueryDataTable : Form
    {

        private Button selectedGroupButton = null;
        private Size ScreenSize;
        public Form_QueryDataTable()
        {
            InitializeComponent();
        }
        private void Form_QueryDataTable_Load(object sender, EventArgs e)
        {

            ScreenSize = Screen.PrimaryScreen.WorkingArea.Size;
            initDataGrid();
            LoadInterface();
            LoadQueryButton();
            
        }

        private void LoadInterface()
        {
            this.Size = new Size((int)(ScreenSize.Width * 0.9f),(int)( ScreenSize.Height * 0.9f));
            this.Location = new Point((ScreenSize.Width - this.Width) / 2,(ScreenSize.Height -this.Height)/2);
            btn_PrintClose.Location = new Point(this.Width - btn_PrintClose.Width, 0);
            cmb_DateList.Location = new Point(btn_PrintClose.Left - cmb_DateList.Width - 5, 2);
            grd_ZoneName.Size = new Size(this.Width, 25);
            grd_ZoneName.Location = new Point(0, lbl_TimeTitle.Top);
            grd_Data.Size = new Size(this.Width, pnl_Equip.Top - grd_ZoneName.Top - 41);
            grd_Data.Location = new Point(-1, grd_ZoneName.Top + 25);
            for (int i = 0; i < QueryForms.fmByDay.DataOfDay.Count ; i++)
            {
                cmb_DateList.Items.Add(QueryForms.fmByDay.DataOfDay[i].QueryDate.ToString("yyyy-MM-dd"));
            }
            if (cmb_DateList.Items.Count > 0) cmb_DateList.SelectedIndex = 0;

            if (cmb_DateList.Items.Count == 1) cmb_DateList.Visible = false;
        }

        private void LoadQueryButton()
        {
            string[] groupid = DBOperate.GetZoneMonitorGroupUsed();
            float v;
            if (groupid == null || groupid.Length <= 0) return;
            DataTable dtGroup = DBOperate.GetDesignGroupSet();
            if (dtGroup == null || dtGroup.Rows.Count <= 0) return;
            for (int i = groupid.Length - 1; i >= 0; i--)
            {
                Button btn = new Button()
                {
                    Size = new Size(150, 50),
                    Text = dtGroup.Select("groupid=" + groupid[i])[0]["groupname"].ToString(),
                    Font = new Font("宋体", 10.5f, FontStyle.Regular),
                    FlatStyle = FlatStyle.Flat,
                    Location = new Point(5 + i * 155, 1),
                    Tag = groupid[i]
                };
                btn.Click += Btn_Click; ;
                pnl_Equip.Controls.Add(btn);
                if (i == 0) selectedGroupButton = btn;
            }
            if (int.TryParse(groupid[0], out int id))
            {
                ShowSelectGroupData(id);
            }
        }

        private void initDataGrid()
        {
            Type dgvtype = grd_Data.GetType();
            PropertyInfo pi = dgvtype.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(grd_Data, true, null);
            int i = 0;
            //for (int i = 0; i < QueryForms.fmquery.netline.zone.Count; i++)
            foreach (var z in QueryForms.fmquery.netline.zone.Values )
            {
                grd_ZoneName.Columns.Add(z.ID.ToString(), z.ZoneName);
                grd_ZoneName.Columns[i].Width = 150;
                grd_ZoneName.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                int totalsubtitles = 0;
                if (z.pv != null)
                {
                    grd_Data.Columns.Add(z.pv.ID.ToString(), "PV");
                    totalsubtitles++;
                }

                if (z.sv != null)
                {
                    grd_Data.Columns.Add(z.sv.ID.ToString(), "SV");
                    totalsubtitles++;
                }
                if (z.ovpv != null)
                {
                    grd_Data.Columns.Add(z.ovpv.ID.ToString(), "OVPV");
                    totalsubtitles++;
                }
                grd_ZoneName.Columns[i].Tag = totalsubtitles;
                int w = 150 / totalsubtitles;
                for (int j = 0; j < totalsubtitles; j++)
                {
                    grd_Data.Columns[grd_Data.Columns.Count - 1 - j].Width = w;
                    grd_Data.Columns[grd_Data.Columns.Count - 1 - j].SortMode = DataGridViewColumnSortMode.NotSortable;
                }
                i++;
            }
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (int.TryParse(btn.Tag.ToString(), out int id))
            {
                ShowSelectGroupData(id);
            }
        }

        private void FillDataIntoGrid(QueryOneDayData DataOfDay)
        {
            //这里需要从数据库中获取数据，还原真实的数据
            grd_Data.Rows.Clear();
            lbl_PrintTitle.Text = DataOfDay.QueryDate.ToString("yyyy年MM月dd日") + " 历史数据";
            DataTable dt = DBOperate.GetParameterData(DataOfDay.QueryDate, DataOfDay.QueryDate.AddDays(1).AddSeconds(-1));
            if (dt == null || dt.Rows.Count == 0) return;
            string[] s = new string[grd_Data.ColumnCount];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataGridViewRow rw = new DataGridViewRow();
                rw.CreateCells(grd_Data);
                if(DateTime.TryParse(dt.Rows[i]["savetime"].ToString(),out DateTime v))
                    rw.HeaderCell.Value = v.ToString("HH:mm:ss");
                for (int j = 0; j < grd_Data.ColumnCount; j++)
                {
                    if (dt.Columns.Contains(grd_Data.Columns[j].Name))
                        s[j] = dt.Rows[i][grd_Data.Columns[j].Name].ToString();
                }
                rw.SetValues(s);
                grd_Data.Rows.Add(rw);
            }
        }

        private void ShowSelectGroupData(int groupid)
        {
            //根据需要进行相关数据和表头的隐藏，以便能够分组显示相关的数据
            int colIndex = 0;
            int i = 0;
            //for (int i = 0; i < QueryForms.fmquery.netline.zone.Count; i++)
            foreach (var z in QueryForms.fmquery.netline.zone.Values )
            {
                int cols = (int)grd_ZoneName.Columns[i].Tag;
                if (z.GroupID == groupid)
                {
                    grd_ZoneName.Columns[i].Visible = true;
                    for (int j = 0; j < cols; j++)
                    {
                        grd_Data.Columns[j + colIndex].Visible = true;
                    }
                }
                else
                {
                    grd_ZoneName.Columns[i].Visible = false;
                    for (int j = 0; j < cols; j++)
                    {
                        grd_Data.Columns[j + colIndex].Visible = false;
                    }
                }
                colIndex += cols;
                i++;
            }
        }

        private void btn_PrintClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void grd_Data_Scroll(object sender, ScrollEventArgs e)
        {
            grd_ZoneName.HorizontalScrollingOffset = grd_Data.HorizontalScrollingOffset;
        }

        private void cmb_DateList_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDataIntoGrid(QueryForms.fmByDay.DataOfDay[cmb_DateList.SelectedIndex]);
        }
    }
}
